package com.qk.management.controller;

import com.qk.common.Result;
import com.qk.common.util.AliYunOSSOperators;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;
import java.util.UUID;

/**
 * @author ZLJ
 * @date 2025/10/8 15:11
 * @Description
 */
@Slf4j
@RestController
@SuppressWarnings("all")
public class FileHandleController {
    @Autowired
    private AliYunOSSOperators oss;

    @PostMapping("/upload")
    public Result upload(@RequestParam("image") MultipartFile image){
        log.info("上传文件：{}",image.getOriginalFilename());
        try{
            Objects.requireNonNull(image,"文件不能为空");
            Objects.requireNonNull(image.getOriginalFilename(),"文件名不能为空");
            return Result.success(oss.upload(image.getBytes(),UUID.randomUUID() + image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf("."))));
        }catch (Exception e){
            log.error("上传文件失败：{}",e.getMessage());
            return Result.error("上传失败");
        }
    }

    /*@PostMapping("/upload")
    public String upload(String username,Integer age,MultipartFile file) throws IOException {
        log.info("用户名：{}，年龄：{}，上传文件：{}",username,age,file.getOriginalFilename());
        file.transferTo(new File("D:\\upload\\"+file.getOriginalFilename()));
        return "上传成功";
    }*/
}
